package com.xdj.controller;

import com.xdj.pojo.Category;
import com.xdj.pojo.Result;
import com.xdj.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: CategoryController
 * Package: com.xdj.controller
 * Description:
 *
 * @Author: 信
 * @Create: 2024/1/11 - 21:05
 */
//@RestController
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增文章分类
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated Category category){
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 文章分类列表
     * @return
     */
    //正确
    @GetMapping
    public Result<List<Category>> list(){
//        List<Category> list = new ArrayList<>();
        List<Category> list = categoryService.list();
        return Result.success(list);
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Category> detail(Integer id){
        Category category = categoryService.detail(id);
        return Result.success(category);
    }

    /**
     * 更新文章分类
     * @param category
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated Category category){
        categoryService.update(category);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Integer id){
        categoryService.delete(id);
        return Result.success();
    }

}
